package com.vankimgps.services;


import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

import com.vankimgps.models.ResponseLogin;
import com.vankimgps.models.ResponseVehicleStatus;
import com.vankimgps.models.ResponseVehicles;
import com.vankimgps.models.UserInfos;
import com.vankimgps.models.VehicleStatus;
import com.vankimgps.models.Vehicles;



public class DataParser {

	public DataParser() {

	}
	
	public static ResponseLogin parseLogin(String xmlString)
			throws XmlPullParserException, IOException{
		ResponseLogin response = new ResponseLogin();
		
	    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	    factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader (xmlString));
        int eventType = xpp.getEventType();
		
        UserInfos userInfo = new UserInfos();
    	String text = "";
		String TagName = "";
		
	    while (eventType != XmlPullParser.END_DOCUMENT) {		    
        	if(eventType == XmlPullParser.START_DOCUMENT) {

        	} else if(eventType == XmlPullParser.END_DOCUMENT) {

        	} else if(eventType == XmlPullParser.START_TAG) {

        		
        	} else if(eventType == XmlPullParser.END_TAG) {
        		TagName = xpp.getName();
        		
        		if (TagName.equalsIgnoreCase("Code"))
        			response.setCode(Integer.parseInt(text));
        		
        		if (TagName.equalsIgnoreCase("Msg"))
        			response.setMsg(text);
        		
        		if (TagName.equalsIgnoreCase("Address"))
        			userInfo.setAddres(text);
        		else if (TagName.equalsIgnoreCase("Contact"))
        			userInfo.setContact(text);
        		else if (TagName.equalsIgnoreCase("NiceName"))
        			userInfo.setNickName(text);
        		else if (TagName.equalsIgnoreCase("Contact"))
        			userInfo.setContact(text);
        		else if (TagName.equalsIgnoreCase("RecID"))
        			userInfo.setRecID(text);
        		else if (TagName.equalsIgnoreCase("Token"))
        			userInfo.setToken(text);

        	} else if(eventType == XmlPullParser.TEXT) {
        		text = xpp.getText();
        	}
        	eventType = xpp.next();
        }
	    response.setUserInfo(userInfo);
		return response;
	}

	public static Object parseVehicle(String xmlString) 	
			throws XmlPullParserException, IOException{{
		// TODO Auto-generated method stub
		ResponseVehicles response = new ResponseVehicles();
		
	    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	    factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader (xmlString));
        int eventType = xpp.getEventType();
		
        Vehicles veh = new Vehicles();
    	String text = "";
		String TagName = "";
		
		List<Vehicles> list = new ArrayList<Vehicles>();
		
	    while (eventType != XmlPullParser.END_DOCUMENT) {		  
        	if(eventType == XmlPullParser.START_DOCUMENT) {

        	} else if(eventType == XmlPullParser.END_DOCUMENT) {

        	} else if(eventType == XmlPullParser.START_TAG) {
        		if (TagName.equalsIgnoreCase("NewestPosition"))
            		veh = new Vehicles();
        	} else if(eventType == XmlPullParser.END_TAG) {
        		TagName = xpp.getName();
        		
        		if (TagName.equalsIgnoreCase("Code"))
        			response.setCode(Integer.parseInt(text));
        		
        		if (TagName.equalsIgnoreCase("Msg"))
        			response.setMsg(text);
        		
        		if (TagName.equalsIgnoreCase("Vehicle")){
        			list.add(veh);
        			 veh = new Vehicles();
        		}
        			
        		if (TagName.equalsIgnoreCase("AuthPwd"))
        			veh.setAuthPwd(text);
        		else if (TagName.equalsIgnoreCase("Cameras"))
        			veh.setCameras(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("CommunicateType"))
        			veh.setCommunicateType(text);
        		else if (TagName.equalsIgnoreCase("FuelConsumption"))
        			veh.setFuelConsumption(text);
        		else if (TagName.equalsIgnoreCase("HaveQualification"))
        			veh.setHaveQualifications(Boolean.parseBoolean(text));
        		else if (TagName.equalsIgnoreCase("IcoEmpty"))
        			veh.setIcoEmpty(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("IcoFault"))
        			veh.setIcoFault(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("IcoHeavy"))
        			veh.setIcoHeavy(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("LastEditTime"))
        			veh.setLastEditTime(text);
        		else if (TagName.equalsIgnoreCase("LastMiles"))
        			veh.setLastMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("MachineType"))
        			veh.setMachineType(text);
        		else if (TagName.equalsIgnoreCase("OperationPwd"))
        			veh.setOperationPwd(text);
        		else if (TagName.equalsIgnoreCase("OwnNo"))
        			veh.setOwnNo(text);
        		else if (TagName.equalsIgnoreCase("RecID"))
        			veh.setRecID(text);
        		else if (TagName.equalsIgnoreCase("Remark"))
        			veh.setRemark(text);
        		else if (TagName.equalsIgnoreCase("SimID"))
        			veh.setSimID(text);
        		else if (TagName.equalsIgnoreCase("StreamLimit"))
        			veh.setStreamLimit(text);
        		else if (TagName.equalsIgnoreCase("SystemNo"))
        			veh.setSystemNo(text);
        		else if (TagName.equalsIgnoreCase("TankType"))
        			veh.setTankType(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("TankTypeRadius"))
        			veh.setTankType(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("TodayMiles"))
        			veh.setTodayMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("VehID"))
        			veh.setVehID(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("VehNoF"))
        			veh.setVehNoF(text);
        		else if (TagName.equalsIgnoreCase("VehNoS"))
        			veh.setVehNoS(text);

        		} else if(eventType == XmlPullParser.TEXT) {
        			text = xpp.getText();
        		}

        	eventType = xpp.next();
	    }
	    response.setList(list);
	    return response;
		}
	}

	public static Object parseVehicleStatus(String xmlString) 	
			throws XmlPullParserException, IOException{{
		// TODO Auto-generated method stub
		ResponseVehicleStatus response = new ResponseVehicleStatus();
		
	    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	    factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader (xmlString));
        int eventType = xpp.getEventType();
		
        VehicleStatus veh = new VehicleStatus();
    	String text = "";
		String TagName = "";
		
		List<VehicleStatus> list = new ArrayList<VehicleStatus>();
		
	    while (eventType != XmlPullParser.END_DOCUMENT) {		  
        	if(eventType == XmlPullParser.START_DOCUMENT) {

        	} else if(eventType == XmlPullParser.END_DOCUMENT) {

        	} else if(eventType == XmlPullParser.START_TAG) {
//        		if (TagName.equalsIgnoreCase("NewestPosition"))
//            		veh = new VehicleStatus();
        	} else if(eventType == XmlPullParser.END_TAG) {
        		TagName = xpp.getName();
        		
        		if (TagName.equalsIgnoreCase("Code"))
        			response.setCode(Integer.parseInt(text));
        		
        		if (TagName.equalsIgnoreCase("Msg"))
        			response.setMsg(text);
        		
        		if (TagName.equalsIgnoreCase("NewestPosition")){
        			list.add(veh);
        			 veh = new VehicleStatus();
        		}
        			
        		if (TagName.equalsIgnoreCase("Angle"))
        			veh.setAngle(Integer.parseInt( text));
        		else if (TagName.equalsIgnoreCase("Fuel"))
        			veh.setFuel(Float.parseFloat(text));
        		else if (TagName.equalsIgnoreCase("LastMiles"))
        			veh.setLastMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("Latitude"))
        			veh.setLatitu(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("Longitude"))
        			veh.setLongitude(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("Miles"))
        			veh.setMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("Oil"))
        			veh.setOil(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("RecID"))
        			veh.setRecID(text);
        		else if (TagName.equalsIgnoreCase("SystemNo"))
        			veh.setSystemNo(text);
        		else if (TagName.equalsIgnoreCase("LastMiles"))
        			veh.setLastMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("Time"))
        			veh.setTime(text);
        		else if (TagName.equalsIgnoreCase("TodayMiles"))
        			veh.setTodayMiles(Double.parseDouble(text));
        		else if (TagName.equalsIgnoreCase("VehID"))
        			veh.setVehID(Integer.parseInt(text));
        		else if (TagName.equalsIgnoreCase("VehNoF"))
        			veh.setVehNoF(text);
        		else if (TagName.equalsIgnoreCase("VehNoS"))
        			veh.setVehNoS(text);

        	} else if(eventType == XmlPullParser.TEXT) {
        			text = xpp.getText();
        	}

        	eventType = xpp.next();
	    }
	    response.setList(list);
	    return response;
		}
	}
}